package com.rinlink.jbcodec.creator;



import com.alibaba.fastjson.JSONObject;
import com.rinlink.jbcodec.JSONCodec;
import com.rinlink.jbcodec.TransformCreator;
import com.rinlink.jbcodec.context.BytesToObjectCreatorContext;
import com.rinlink.jbcodec.transform.Transform;

import java.nio.ByteBuffer;

public abstract class ByteBufferToObjectCreator<T> implements TransformCreator<T,BytesToObjectCreatorContext> {

    @Override
    public Transform<T> create(int fieldIndex, BytesToObjectCreatorContext ctx) {
        JSONCodec.Field field = ctx.getScheme().getFields().get(fieldIndex);
        ByteBuffer sourceData = (ByteBuffer) ctx.getSourceData();
        JSONObject targetData = (JSONObject) ctx.getTargetData();
        JSONObject attr = field.getAttr();
        return create(attr == null ? new JSONObject() : attr,sourceData,targetData);
    }

    protected abstract Transform<T> create(JSONObject attr, ByteBuffer data, JSONObject t);

}
